<?php
/**
 * jishupu-cms
 * @Author:技术铺
 * @Date:2021/11/22
 */

namespace App\Services;

use App\Models\ViewLog;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

/**
 * 应用访问量模块服务层
 *
 * Class ViewLogService
 * @package App\Services
 */
class ViewLogService
{

    /**
     * 日访问量统计
     * @param int $day 周期
     * @return array
     */
    public static function dailyStatistics($day = 7)
    {
        $actual_day = $day - 1;
        $start_date = Carbon::now()->modify("-{$actual_day} days")->toDateString() . " 00:00:00";
        $end_date = Carbon::now()->toDateString() . " 23:59:59";
        $view_log = ViewLog::whereBetween('created_at', [$start_date, $end_date])
            ->select('app_type', DB::raw('DATE(created_at) as date'), DB::raw('sum(hits) as value'))
            ->groupBy(['app_type', 'date'])->get();

        // 应用类型名称列表
        $legendData = DictionaryService::getItemsNamesArray('appTypeVar');
        $appTypeVar = DictionaryService::getItemsValuesArray('appTypeVar');

        // x坐标日期列表
        $xdate = [];
        for ($i = $actual_day; $i >= 0; $i--) {
            $xdate[] = Carbon::now()->modify("-{$i} days")->toDateString();
        }
        // x坐标数据列表
        $xdata = [];
        if (!empty($appTypeVar)) {
            foreach ($appTypeVar as $type_key => $type) {
                $xdata[$type_key] = [
                    'name' => DictionaryService::getItemName('appTypeVar', $type),
                    'data' => []
                ];
                foreach ($xdate as $xdate_key => $xdate_value) {
                    $xdata[$type_key]['data'][$xdate_key] = 0;
                    foreach ($view_log as $item) {
                        if ($type == $item['app_type'] && $xdate_value == $item['date']) {
                            $xdata[$type_key]['data'][$xdate_key] = $item['value'];
                        }
                    }
                }
            }
        }
        $yMax = $view_log ? collect($view_log)->max('value') : 0;
        if ($yMax > 0) {
            $yMaxStr = (string)$yMax;
            $yMaxLength = strlen(floor($yMaxStr));
            $yMax = str_pad($yMaxStr[0], $yMaxLength, '0');
            if ($yMax < $yMaxStr) {
                $yMax = str_pad($yMaxStr[0] + 1, $yMaxLength, '0');
            }
        }
        $yMax = $yMax < 100 ? 100 : $yMax;
        return [
            'day' => $day,
            'legendData' => $legendData,
            'xdate' => $xdate,
            'xdata' => $xdata,
            'ymin' => 0,
            'ymax' => $yMax,
        ];
    }


    /**
     * 客户端设备操作系统统计
     * @param int $day 周期
     * @return array
     */
    public static function platformStatistics($day = 7)
    {
        $view_log = ViewLog::select('request_platform', DB::raw('sum(hits) as value'));
        if ($day > 0) {
            $actual_day = $day - 1;
            $start_date = Carbon::now()->modify("-{$actual_day} days")->toDateString() . " 00:00:00";
            $end_date = Carbon::now()->toDateString() . " 23:59:59";
            $view_log->whereBetween('created_at', [$start_date, $end_date]);
        }
        $result = $view_log->groupBy(['request_platform'])->get();
        return collect($result)->map(function ($item) {
            $name = $item->request_platform ?: 'unknown';
            return [
                'name' => $name,
                'value' => $item->value
            ];
        })->toArray();
    }


    /**
     * 客户端浏览器统计
     * @param int $day 周期
     * @return array
     */
    public static function browserStatistics($day = 7)
    {
        $view_log = ViewLog::select('request_browser', DB::raw('sum(hits) as value'));
        if ($day > 0) {
            $actual_day = $day - 1;
            $start_date = Carbon::now()->modify("-{$actual_day} days")->toDateString() . " 00:00:00";
            $end_date = Carbon::now()->toDateString() . " 23:59:59";
            $view_log->whereBetween('created_at', [$start_date, $end_date]);
        }
        $result = $view_log->groupBy(['request_browser'])->get();
        return collect($result)->map(function ($item) {
            return [
                'name' => $item->request_browser,
                'value' => $item->value
            ];
        })->toArray();
    }


}
